﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharatorMono : MonoBehaviour {
    Rigidbody2D Rigid;
    public bool IsJump;
    public float JumpSpeed=5;
    public float LastY;
    public float HorizenSpeed = 0.1f;
    public Vector3 Postion;
    public float MaxY=-100;
    BoxCollider2D col;
	// Use this for initialization
	void Start () {
        Rigid = GetComponent<Rigidbody2D>();
        col = GetComponent<BoxCollider2D>();
        LastY = transform.position.y;
        
    }
	
	// Update is called once per frame
	void Update () {

        if (Input.GetKeyDown(KeyCode.Space))
        {
            Jump();
        }
    }

    private void Jump()
    {
        Rigid.AddForce(Vector2.up * JumpSpeed);
    }

    void FixedUpdate()
    {
        Postion = transform.position;
        if (Postion.y > MaxY)
        {
            MaxY = Postion.y;
        }
        if (LastY < transform.position.y)
        {
            IsJump = true;
            col.enabled = false;
        }
        else
        {
            IsJump = false;
            col.enabled = true;
        }
        LastY = transform.position.y;

        if (Input.GetAxis("Horizontal") !=0)
        {
            transform.position += Vector3.right * Input.GetAxis("Horizontal")* HorizenSpeed;
        }
        if (transform.position.x > 3)
        {
            transform.position -= Vector3.right * 6;
        }
        else if(transform.position.x < -3)
        {
            transform.position += Vector3.right * 6;
        }
    }
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.name.Equals("Bottom_Rect"))//死亡
        {
            print("dead!!!!!!!!!");
        }
 
        if(!IsJump&&transform.position.y- collision.transform.position.y>0.1f)
        Rigid.AddForce(Vector2.up * JumpSpeed);
    }
}

